package com.crane.oss.web.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.Response;
import com.crane.common.core.dto.SessUser;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.oss.sys.bean.request.menu.AddMenuRequest;
import com.crane.oss.sys.bean.request.menu.QueryMenuRequest;
import com.crane.oss.sys.bean.request.menu.QueryMenuTreeRequest;
import com.crane.oss.sys.bean.request.menu.UpdateMenuRequest;
import com.crane.oss.sys.bean.resp.MenuBean;
import com.crane.oss.sys.bean.resp.MenuVueEleBean;
import com.crane.oss.sys.bean.resp.TreeMenuBean;
import com.crane.oss.enums.UserTypeEnum;
import com.crane.oss.sys.service.MenuService;
import com.crane.oss.sys.service.MenuVueEleService;
import com.crane.oss.web.BaseBusinessController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2018年12月28日 19时42分58秒
 */

@Api(value = "MenuController", tags = {"系统管理-菜单管理"})
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends BaseBusinessController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuVueEleService menuVueEleService;

    @ApiOperation(value = "获取菜单按钮列表信息", notes = "获取菜单按钮列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryMenuRequest")
    @RequiresPermissions("sys:menu:list")
    @PostMapping("/list")
    public MultiResponse list(@RequestBody QueryMenuRequest request) {
        // 查询列表数据
        SessUser user = getUser();
        if (!user.getUserType().equals(UserTypeEnum.SUPER_ADMIN.getType())) {
            return MultiResponse.buildFailure(BasicErrorCode.NO_DATA_RIGHT);
        }
        ;
        IPage<MenuBean> page = menuService.queryList(request);
        return MultiResponse.of(page);
    }

    @ApiOperation(value = "获取菜单按钮包含接口信息", notes = "获取菜单按钮包含接口信息")
    @RequiresPermissions("sys:menu:info")
    @PostMapping("/info")
    public SingleResponse info(@RequestBody String data) {
        String sid = getParamter(data, "id");
        MenuBean menuBean = menuService.queryMenu(Long.parseLong(sid));
        return SingleResponse.of(menuBean);
    }

    @ApiOperation(value = "获取用户所有菜单树信息", notes = "菜单管理菜单树")
    @RequiresPermissions("sys:menu:treeList")
    @GetMapping("/menuTreeList")
    public MultiResponse menuTreeList() {
        QueryMenuTreeRequest queryMenuTreeRequest = new QueryMenuTreeRequest();
        queryMenuTreeRequest.setHasButton(false);
        TreeMenuBean treeMenuBean = menuService.queryMenuTree(queryMenuTreeRequest, getUser());
        List<TreeMenuBean> list = new ArrayList<>();
        list.add(treeMenuBean);
        return MultiResponse.ofWithoutTotal(list);
    }

    @ApiOperation(value = "获取用户角色树信息", notes = "角色管理菜单树")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryMenuTreeRequest")
    @RequiresPermissions("sys:menu:treeList")
    @PostMapping("/roleTreeList")
    public MultiResponse roleTreeList(@RequestBody QueryMenuTreeRequest request) {
        SessUser user = getUser();
        if (!user.getUserType().equals(UserTypeEnum.SUPER_ADMIN.getType()) && request.getRoleId() == null) {
            return MultiResponse.buildFailure("角色不能为空");
        }
        ;
        request.setHasButton(true);
        TreeMenuBean treeMenuBean = menuService.queryMenuTree(request, getUser());
        List<TreeMenuBean> list = new ArrayList<>();
        list.add(treeMenuBean);
        return MultiResponse.ofWithoutTotal(list);
    }

    @ApiOperation(value = "获取路由信息", notes = "获取路由信息")
    @GetMapping("/routeList")
    @RequiresAuthentication
    public SingleResponse routeList() {
        MenuVueEleBean menuVueEleBean = menuVueEleService.queryMenuVueTree(getUser());
        return SingleResponse.of(menuVueEleBean);
    }

    @ApiOperation(value = "新增菜单按钮", notes = "获取新增信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddMenuRequest")
    @RequiresPermissions("sys:menu:save")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid AddMenuRequest request) {
        SessUser user = getUser();
        if (!user.getUserType().equals(UserTypeEnum.SUPER_ADMIN.getType())) {
            return Response.buildFailure(BasicErrorCode.NO_DATA_RIGHT);
        }
        ;
        menuService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改菜单按钮信息", notes = "修改菜单按钮信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateMenuRequest")
    @RequiresPermissions("sys:menu:update")
    @PostMapping("/update")
    public Response update(@RequestBody @Valid UpdateMenuRequest request) {
        SessUser user = getUser();
        if (!user.getUserType().equals(UserTypeEnum.SUPER_ADMIN.getType())) {
            return Response.buildFailure(BasicErrorCode.NO_DATA_RIGHT);
        }
        ;
        menuService.update(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequiresPermissions("sys:menu:delete")
    @PostMapping("/delete")
    public Response delete(@RequestBody String data) {
        String sid = getParamter(data, "id");
        SessUser user = getUser();
        if (!user.getUserType().equals(UserTypeEnum.SUPER_ADMIN.getType())) {
            return Response.buildFailure(BasicErrorCode.NO_DATA_RIGHT);
        }
        ;
        menuService.delete(Long.parseLong(sid));
        return Response.buildSuccess();
    }

}
